//
//  UIImageView+HMDownloadIMG.m
//  仿写SDWebImage
//
//  Created by 倪啸天 on 16/10/15.
//  Copyright © 2016年 itheima. All rights reserved.
//

#import "UIImageView+HMDownloadIMG.h"
#import <objc/runtime.h>
#import "NSOperationManager.h"

@implementation UIImageView (HMDownloadIMG)


- (void)setCurrentUrlString:(NSString *)currentUrlString {

    objc_setAssociatedObject(self, "key", currentUrlString, OBJC_ASSOCIATION_COPY_NONATOMIC);
}

- (NSString *)currentUrlString {

    return  objc_getAssociatedObject(self,"key");
    
}

- (void)HM_downloadIMGWithUrlString:(NSString *)urlString {
    
    //取消
    if (![self.currentUrlString isEqualToString:urlString]) {
        [[NSOperationManager shareManager] cancelOperationWithUrlString:self.currentUrlString];
    }
    
    self.currentUrlString = urlString;
    
    //下载
    [[NSOperationManager shareManager] downloadIMGWithUrlString:urlString andCompletedBlock:^(UIImage *image) {
        self.image = image;
    }];
    
}

@end
